package com.kzd.btgame.main

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.*
import com.kzd.btgame.R
import com.kzd.btgame.app.URL_REBATE
import com.kzd.btgame.app.SPSConstance.uid
import com.kzd.btgame.app.SPSConstance.userInfo
import com.kzd.btgame.app.SPSConstance.userMine
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.FragmentMainMineBinding
import com.kzd.btgame.entity.UserInfo
import com.kzd.btgame.entity.UserMine
import com.kzd.btgame.jetpack.vm.MineViewModel
import com.kzd.btgame.main.mine.*
import com.kzd.btgame.utils.httpSign1
import com.kzd.btgame.utils.httpTime
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel
import java.lang.StringBuilder

class MainMineFragment : OwnerViewBindingFragment<FragmentMainMineBinding>() {

    companion object {
        fun newInstance(): MainMineFragment {
            return MainMineFragment()
        }
    }

    private val mViewModel: MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentMainMineBinding
        get() = FragmentMainMineBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.includeMainMineBar.root
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        //消息中心
        mViewBinding.includeMainMineBar.ivMainMineMessage.setOnClickAntiShakeListener {
            toMineMessageActivity()
        }

        //设置中心
        mViewBinding.includeMainMineBar.ivMainMineSetting.setOnClickAntiShakeListener {
            toMineSettingActivity()
        }

        //用户中心
        mViewBinding.includeMainMine1.ivMainMineUserHeader.setOnClickAntiShakeListener {
            toMineUserActivity()
        }
        mViewBinding.includeMainMine1.llMainMineUserInfo.setOnClickAntiShakeListener {
            toMineUserActivity()
        }

        //每日签到
        mViewBinding.includeMainMine1.tvMainMineSign.setOnClickAntiShakeListener {
            toMainActivity(index = 2)
        }

        //我的积分
        mViewBinding.includeMainMine2.ivMainMineMall.setOnClickAntiShakeListener {
            toMoneyMallListActivity()
        }

        //平台币
        mViewBinding.includeMainMine2.tvMainMineCurrency.setOnClickAntiShakeListener {
            toMinePlatformActivity()
        }

        //会员-vip
        mViewBinding.includeMainMine3.clMainMineMember1.setOnClickAntiShakeListener {
            toMineMemberActivity(index = 1)
        }

        //会员-省钱卡
        mViewBinding.includeMainMine3.clMainMineMember2.setOnClickAntiShakeListener {
            toMineMemberActivity(index = 2)
        }

        //申请返利
        mViewBinding.includeMainMine4.tvMainMineRebate.setOnClickAntiShakeListener {
            val userId = userInfo?.user_id ?: ""
            val url = StringBuilder(URL_REBATE)
                .append("&uid=").append(userId)
                .append("&t=").append(httpTime)
                .append("&sign=").append(httpSign1(userId))
                .toString()
            toMineRichTextActivityUrl(title = "返利中心", url = url, isLogin = true)
        }

        //代金券
        mViewBinding.includeMainMine4.tvMainMineCoupon.setOnClickAntiShakeListener {
            toMineCouponActivity()
        }

        //我的礼包
        mViewBinding.includeMainMine4.tvMainMineGiving.setOnClickAntiShakeListener {
            toMineGiftActivity()
        }

        //我的足迹
        mViewBinding.includeMainMine4.tvMainMineFootprint.setOnClickAntiShakeListener {
            toMineFootprintActivity()
        }

        //积分抽奖
        mViewBinding.includeMainMine4.tvMainMineIntegral.setOnClickAntiShakeListener{

        }

        //开服表
        mViewBinding.includeMainMine4.tvMainMineService.setOnClickAntiShakeListener{
            toHomeServiceActivity()
        }

        //充值转游
        mViewBinding.includeMainMine4.tvMainMineTransfer.setOnClickAntiShakeListener {
            toMineTransferActivity()
        }

        //小号回收
        mViewBinding.includeMainMine4.tvMainMineRecycle.setOnClickAntiShakeListener {
            toBargainBuyActivity()
        }

        //我的账单
        mViewBinding.includeMainMine5.tvMainMineBill.setOnClickAntiShakeListener {
            toMineRecordBillActivity()
        }

        //收藏中心
        mViewBinding.includeMainMine5.tvMainMineCollect.setOnClickAntiShakeListener {
            toMineCollectActivity()
        }

        //投诉反馈
        mViewBinding.includeMainMine5.tvMainMineFeedback.setOnClickAntiShakeListener {
            toMineFeedbackActivity()
        }

        //联系客服
        mViewBinding.includeMainMine5.tvMainMineContact.setOnClickAntiShakeListener {
            toMainActivity(index = 3)
        }

        //邀请好友
        mViewBinding.includeMainMine5.tvMainMineInvite.setOnClickAntiShakeListener {
            toMineInviteActivity()
        }

//
//        //下载管理
//        mViewBinding.tvMainMineDownload.setOnClickAntiShakeListener {
//            toMineDownloadActivity()
//        }
//
//        //账号安全
//        mViewBinding.tvMainMineAccount.setOnClickAntiShakeListener {
//            toMineUserActivity()
//        }
//
//        //加入qq
//        mViewBinding.tvMainMineJoinQQ.setOnClickAntiShakeListener {
//            toExternalAppQQ(requireActivity())
//        }


    }


    override fun initData() {
        mViewModel.userAppTips()
    }

    override fun onResume() {
        super.onResume()
        fillData(userInfo, userMine)
        mViewModel.userInfo()
        mViewModel.userMine()
    }

    override fun initViewMode() {
        mViewModel.userInfoLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data, userMine)
        })
        mViewModel.userMineLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(userInfo, data)
        })

        BaseViewModel.userAppTipsLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mViewBinding.includeMainMineBar.viewMainMineMessageRead.visibility =
                if (data.msg_rs != 1) View.GONE else View.VISIBLE
        })
    }

    private fun fillData(userInfo: UserInfo?, userMine: UserMine?) {

        mViewBinding.includeMainMine1.ivMainMineUserHeader.loadCircleImage(
            if (userInfo?.face.isNullOrEmpty())
                R.mipmap.ic_test_1 else userInfo?.face ?: R.mipmap.ic_test_1
        )

        mViewBinding.includeMainMine1.tvMainMineUserPhone.visibility =
            if (uid.isNullOrEmpty()) View.GONE else View.VISIBLE

        mViewBinding.includeMainMine1.tvMainMineUserNickname.text = when {
            uid.isNullOrEmpty() -> "未登录"
            userInfo?.pet_name.isNullOrEmpty() -> userInfo?.user_name
            else -> userInfo?.pet_name
        }

        mViewBinding.includeMainMine1.tvMainMineUserPhone.text =
            if (!userInfo?.mobile.isNullOrEmpty()) userInfo?.mobile else "未绑定"

        mViewBinding.includeMainMine2.tvMainMineNumPlatform.text =
            userMine?.coin ?: "0.00"
        mViewBinding.includeMainMine2.tvMainMineNumIntegral.text =
            userMine?.points ?: "0.00"
    }
}